package cn.com.captureNetPacket.voice;

import org.jnetpcap.protocol.voip.Rtcp;
import org.jnetpcap.protocol.voip.RtcpSDES;
import org.jnetpcap.protocol.voip.RtcpSDES.RtcpSDESChunk;

import cn.com.captureNetPacket.model.NetworkInfo;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class AnalysisRTCP {
	RtcpSDES rtcpSDES;Rtcp rtcpBye;NetworkInfo networkInfo;String name=null;
	
	public AnalysisRTCP(NetworkInfo networkInfo, RtcpSDES rtcpSDES,Rtcp rtcpBye){
		this.rtcpSDES=rtcpSDES;
		this.rtcpBye=rtcpBye;
		this.networkInfo=networkInfo;
	}
	
	
	public AnalysisRTP getAnalysisRTP(NetworkInfo networkInfo){
		AnalysisRTP analysisRTP=null;
		NetworkInfo rtpNetworkInfo=new NetworkInfo(networkInfo.getSourceIP(), networkInfo.getDestinationIP(), networkInfo.getSourceMac(), networkInfo.getDestinationMac(), networkInfo.getTransportLayer(), networkInfo.getSourcePort()-1, networkInfo.getDestinationPort()-1);
		analysisRTP=AnalysisRTP.getAnalysisRTP(rtpNetworkInfo);
		return analysisRTP;
	}
	
	public void  getRtcpSDESItem(){
		 try{
	         RtcpSDES.RtcpSDESChunk[] rl=rtcpSDES.chunks();
	         for (RtcpSDESChunk rtcpSDESChunk : rl) {
	        	 log.info("rtcpSDESChunkInfo sourceIP:{} port{} mac:{}->destinationIP:{}  port{}  mac:{} item：{} itemCount:{}",networkInfo.getSourceIP(), networkInfo.getSourcePort() , networkInfo.getSourceMac(),networkInfo.getDestinationIP(), networkInfo.getDestinationMac(), networkInfo.getDestinationPort(),rtcpSDESChunk.toString(),rl.length);
	        	   getName(rtcpSDESChunk.toString()); 
	        	   }
	            	 }catch (Exception e) {
	            		 log.error("非常规RTCP error sourceIP:{} port{} mac:{}->destinationIP:{}  port{}  mac:{} errmessage:",networkInfo.getSourceIP(), networkInfo.getSourcePort() , networkInfo.getSourceMac(),networkInfo.getDestinationIP(), networkInfo.getDestinationMac(), networkInfo.getDestinationPort(),e);
					}
	}
	
	
	public void startAnalysisRTP(){
		AnalysisRTP analysisRTP=null;
		analysisRTP=getAnalysisRTP(this.networkInfo);
		getRtcpSDESItem();
		if(analysisRTP!=null){
			//设置item里面的名字
			 
			//收到结束事件
			if(rtcpBye!=null){
//				analysisRTP.RtcpGoodbye=true;
				analysisRTP.setStopThread(true);;
				log.info("RtcpGoodbye sourceIP:{} port{} mac:{}->destinationIP:{}  port{}  mac:{} ",networkInfo.getSourceIP(), networkInfo.getSourcePort() , networkInfo.getSourceMac(),networkInfo.getDestinationIP(), networkInfo.getDestinationMac(), networkInfo.getDestinationPort());
			}
	        	
		}
		 
		
	}
	
	
	public void getName(String Item){
		try{
		String[] temp,temp2;
		temp=Item.split(",");
		if(temp!=null)
		for(String s : temp){
			temp2=s.split("@");
			if(temp2!=null){
				name=temp2[0];
			}
		}
		}catch (Exception e) {
			 log.error("get Name error sourceIP:{} port{} mac:{}->destinationIP:{}  port{}  mac:{} getName：{} errmessage:",networkInfo.getSourceIP(), networkInfo.getSourcePort() , networkInfo.getSourceMac(),networkInfo.getDestinationIP(), networkInfo.getDestinationMac(), networkInfo.getDestinationPort(),Item,e);
      	}
	}
	
	
}
